package com.aptemo.webetek.server.handlers;

import com.aptemo.webetek.client.commands.Action;
import com.aptemo.webetek.client.commands.impl.CreateEventAction;
import com.aptemo.webetek.client.exceptions.BackendException;
import com.aptemo.webetek.client.exceptions.WebetekLoginException;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.shared.EventBean;

public class CreateEventHandler
		extends ActionHandler<CreateEventAction, EventBean>
{

	public CreateEventHandler(DataStoreAPI service)
	{
		super(service);
	}

	public Class<? extends Action<?>> getActionType()
	{
		return CreateEventAction.class;
	}

	@Override
	public EventBean invoke(CreateEventAction action) throws BackendException, WebetekLoginException
	{
		utils.checkLogin();
		
		if (!utils.isTeamEditor(action.getTeamId()))
		{
			throw new BackendException(EErrorCodes.NOT_TEAM_EDITOR);
		}
	
		EventBean result = dataService.createEvent(action.getTeamId(), action.getTitle(), action.getDescription(), action.getDate(), action.getPicks(), action
				.getBetType(), action.getLabelId());
		return result;
	}

}
